<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace plugins\userrecharge\api\controller;

use app\api\BaseController;
use app\api\model\User;
use plugins\pay\addons\Alipay;
use plugins\pay\addons\Wechat;
use plugins\userrecharge\api\model\UserRecharge;
/**
 * 会员充值
 */
class Notify extends BaseController
{
    /**
     * 任务计划处理过期订单
     */
    public function taskPlan()
    {
        UserRecharge::whereTime('create_time', '<', time() - (60*30))->where('status', 1)->update(['status' => 4]);
    }
    
	/**
     * 支付宝支付通知(异步)
     */
    public function alipayPayNotify()
    {
        $input = input();
        if (empty($input['out_trade_no'])) {
            echo 'fail';
        }
        $order = UserRecharge::where('id', $input['out_trade_no'])->where('status', 1)->find();
        if (empty($order)) {
            echo 'fail';
        }
        $result = Alipay::query($input['out_trade_no']);
        if ($result['status'] != 'success') {
            echo 'fail';
        }
        $order->pay_method = 0;
        $order->pay_time   = date('Y-m-d H:i:s');
        $order->status     = 3;
        $order->save();
        User::where('id', $order->user_id)->inc('balance', $order->pay_rmb)->update();
        echo 'success';
        
    }

    /**
     * 微信支付通知(异步)
     */
    public function wechatPayNotify()
    {
        $xml   = json_encode(simplexml_load_string(input(), 'SimpleXMLElement', LIBXML_NOCDATA));
        $input = json_decode($xml, true);
        if (empty($input['out_trade_no'])) {
            echo '<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[订单不存在]]></return_msg></xml>';
        }
        $order = UserRecharge::where('id', $input['out_trade_no'])->where('status', 1)->find();
        if (empty($order)) {
            echo '<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[订单不存在]]></return_msg></xml>';
        }
        $query = Wechat::query($input['out_trade_no']);
        if ($query['status'] != 'success') {
            echo '<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[订单不存在]]></return_msg></xml>';
        }
        $order->pay_method = 1;
        $order->pay_time = date('Y-m-d H:i:s');
        $order->status = 3;
        $order->save();
        User::where('id', $order->user_id)->inc('balance', $order->pay_rmb)->update();
        echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
    }
}